﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2.CLASS
{
    public class Product
    {
         #region " & PropertyS & "
        private string _ProductId;
        public string ProductId
        {
            get { return _ProductId; }
            set { _ProductId = value; }
        }
        private string _ProductName;
        public string ProductName
        {
            get { return _ProductName; }
            set { _ProductName = value; }
        }
        private string _ProductTypeId;
        public string ProductTypeId
        {
            get { return _ProductTypeId; }
            set { _ProductTypeId = value; }
        }
        private string _ProductSize;
        public string ProductSize
        {
            get { return _ProductSize; }
            set { _ProductSize = value; }
        }
        private string _ProductPrice;
        public string ProductPrice
        {
            get { return _ProductPrice; }
            set { _ProductPrice = value; }
        }
        private string _ProductToo;
        public string ProductToo
        {
            get { return _ProductToo; }
            set { _ProductToo = value; }
        }
        #endregion
        public Product()
        {
            //butsets

        }
        public Product(string _ProductId, string _ProductName, string _ProductTypeId, string _ProductSize, string _ProductPrice, string _ProductToo)
        {
            this.ProductId = _ProductId;
            this.ProductName = _ProductName;
            this.ProductTypeId = _ProductTypeId;
            this.ProductSize = _ProductSize;
            this.ProductPrice = _ProductPrice;
            this.ProductToo = _ProductToo;
        }
        public static Product getbyRec(string _byProductId)
        {
            Product x = new Product();

            string qry1 = string.Format("select *from  Product where ProductId=('{0}')", _byProductId);
            DataTable table = DBTools.selecQryTmp(qry1);

            try
            {
                return new Product(table.Rows[0]["ProductId"].ToString(), table.Rows[0]["ProductName"].ToString(), table.Rows[0]["ProductTypeId"].ToString(), table.Rows[0]["ProductSize"].ToString(), table.Rows[0]["ProductPrice"].ToString(), table.Rows[0]["ProductToo"].ToString());


            }
            catch (Exception e)
            {
                //MsgBox(ex.Message, MsgBoxStyle.OkCancel, "account->getAccountByRecID")'
                MessageBox.Show(string.Format("{0} Exception caught.", e));

                return null;
            }



        }
        public static bool AddFunction(string _ProductId, string _ProductName, string _ProductTypeId, string _ProductSize, string _ProductPrice, string _ProductToo)
        {

           // DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureProduct";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductId", SqlDbType.NVarChar, 10)).Value = _ProductId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, 10)).Value = _ProductName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductTypeId", SqlDbType.NVarChar, 55)).Value = _ProductTypeId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductSize", SqlDbType.NVarChar, 10)).Value = _ProductSize;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductPrice", SqlDbType.NVarChar, 10)).Value = _ProductPrice;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductToo", SqlDbType.NVarChar, 55)).Value = _ProductToo;

            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "ADD";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;


        }
        public static bool EditFunction(string _byProductId, string _byProductName, string _byProductTypeId, string _byProductSize, string _byProductPrice, string _byProductToo)
        {


            DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureProduct";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductId", SqlDbType.NVarChar, 10)).Value = _byProductId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, 10)).Value = _byProductName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductTypeId", SqlDbType.NVarChar, 55)).Value = _byProductTypeId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductSize", SqlDbType.NVarChar, 10)).Value = _byProductSize;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductPrice", SqlDbType.NVarChar, 10)).Value = _byProductPrice;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductToo", SqlDbType.NVarChar, 55)).Value = _byProductToo;

            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NChar, 4)).Value = "EDIT";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;


        }

        public static bool DelFunction(string _byProductId)
        {


           // DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureProduct";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductId", SqlDbType.NVarChar, 10)).Value = _byProductId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductTypeId", SqlDbType.NVarChar, 55)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductSize", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductPrice", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductToo", SqlDbType.NVarChar, 55)).Value = "";

            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "DEl";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;


        }
    }
}
